<?php
/**
 * Display Tasks Summary
 */

// Load User Current open task
use Fox\Behaviors;
use Fox\Dao\SqlFragment;
use Fox\Generator;

$xview = Xview::instance();
$maxResults = $xview->val('maxresults',10);
// Load all Tasks
$query = Fox\Dao::infos(\Shama\Task::className())
			->handle()->createQueryBuilder();
			
$query->select('t.taskid','status','priority','subject','estimate_duedate','reference','reference_id')
		->from('tasks','t')
		->leftJoin('t','task_references','r','t.taskid = r.taskid')
		->setMaxResults($maxResults);

$fragments[] = new SqlFragment('assigned_to = ? AND status != ? AND loekz = ?',
							array(User::current()->getId(),\Shama\Task::STATUS_CLOSED,0));
$fragment = SqlFragment::combine($fragments);
$tasks = array();
$tasksReferences = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);
foreach($tasksReferences as $referenceData) {
	$taskid = $referenceData['taskid'];
	$reference = $referenceData['reference'];
	$reference_id = $referenceData['reference_id'];
	if(!isset($tasks[$taskid])) {
		$tasks[$taskid] = $referenceData;
	}
	$tasks[$taskid]['references'][$reference] = $reference_id;
}
// Load All task priorities
$priorities = Fox\Dao::finder(Shama\Task\Priority::className())
				->where('language = ?',array($xview->language()))
				->find();
foreach($priorities as $priority) {
	$prioritiesLabel[$priority->priority] = $priority->label;
}

$block = new Fox\Html\Block('bTasks',Msg(Hive::BUNDLE,Hive::TITLE_TASKS));
$grid = new Fox\Grid('gTasks');
$grid->fieldcat(array(
	array('task_label','subject'),
	array('task_priority','priority'),
	array('date','estimate_duedate'),
	'idele'
));
$rownb = 0;
$grid->fieldcat()->field('idele')
		->setGenerator(Generator::useValue('?'))
		->label(Msg(Hive::BUNDLE,Hive::MARK_AS_DONE));
$grid->fieldcat()->field('priority')
	->align('center')
	->setGenerator(Generator::useFunction(function($data) use($prioritiesLabel){
		return $prioritiesLabel[$data['priority']];
	}));
$grid->fieldcat()->field('idele')->renderer()->behaviors(
	Behaviors::action(
		Generator::url('tasks','tickmark',null,
						array('taskid' => Generator::useElement('taskid'))),
		false,
		null,
		null,
		false,
		'POST',
		null,
		Generator::useFunction(function($data) use(&$rownb) {
			$rownb++;
			return '#gTasks_'.$rownb;
		}),
		null,
		true
));

$grid->onClick(Generator::location(
	Generator::useFunction(function($data){
		if(!empty($data['references'][Hive\Mission::TASK_REFERENCE])) {
			return '?mod=missions&act=main&mid='.$data['references'][Hive\Mission::TASK_REFERENCE];
		} elseif(!empty($data['references'][Hive\Ressource::TASK_REFERENCE])) {
			return '?mod=ressource&act=main&uri=ressource-tasks&rid='.$data['references'][Hive\Ressource::TASK_REFERENCE];
		} elseif(!empty($data['references'][Hive\Company::TASK_REFERENCE])) {
			return '?mod=company&act=main&uri=company-tasks&cpid='.$data['references'][Hive\Company::TASK_REFERENCE];
		}
	}
)));
$grid->initialize($tasks);
if($xview->isPrintMode()) {
	$grid->layout()->scrollVertical(true);
	return $grid->display();
} else {
	$block->add($grid);
	$block->collapsed(false);
	echo $block->display();
}
